﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Seagull.BarTender.Print;
using System.IO;
using System.Configuration;

namespace POSPlus.EZBarTender
{
    /// <summary>
    /// Generates a print preview image of the labels you will be printing, and is used as
    /// confirmation that the user has put the correct paper in the printer.
    /// </summary>
    public partial class PrintPreview : Form
    {
        /// <summary>
        /// Location of the print preview XML document.
        /// </summary>
        public String XmlPreviewLocation { get; private set; }

        /// <summary>
        /// Default constructor built by the Visual Studio 2010 form generator.
        /// </summary>
        public PrintPreview()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Calls the default constructor provided by Visual Studio then
        /// assigns <code>XmlPreviewLocatoin</code> with the provided param.
        /// </summary>
        /// <param name="xmlPreviewLocation"></param>
        public PrintPreview(string xmlPreviewLocation) : this()
        {
            XmlPreviewLocation = xmlPreviewLocation;
        }

        /// <summary>
        /// Gets the available printers on the machine then puts them into a combobox
        /// with the default selected.  Turns on the updating image disables the combobox 
        /// and the print button then starts the background worker to generate the preview.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintPreview_Load(object sender, EventArgs e)
        {
            // Get the printers and put them in a combobox
            Printers printers = new Printers();
            foreach (Printer printer in printers)
            {                
                _comboBoxPrinter.Items.Add(printer.PrinterName);
            }

            // Select the default printer
            if (printers.Count > 0)
                _comboBoxPrinter.SelectedItem = printers.Default.PrinterName;

            // Show the updating picture, disable combobox and print button
            _pictureBoxUpdating.Visible = true;
            _comboBoxPrinter.Enabled = false;
            _buttonPrint.Enabled = false;

            // Start the background worker
            _backgroundWorker.RunWorkerAsync();            
        }

        /// <summary>
        /// Starts the BarTender Engine and executes the XML Script at the preview location.
        /// Then stops the engine
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            using (Engine engine = new Engine(true))
            {
                engine.XMLScript(XmlPreviewLocation, XMLSourceType.ScriptFile);
                engine.Stop();
            }
        }

        /// <summary>
        /// Sets the combobox and print button to enabled. Shows the preview and hides the 
        /// updating image
        /// </summary>
        /// 
        /// <exception cref="ConfigurationErrorsException">
        /// Thrown if the 'PrintPreviewFolder' is not found in the application configuration file.
        /// </exception> 
        /// 
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Enable combobox and print button
            _comboBoxPrinter.Enabled = true;
            _buttonPrint.Enabled = true;

            try
            {
                // Set the image location for the print preview
                _pictureBoxPreview.ImageLocation = ConfigurationManager.AppSettings["PrintPreviewFolder"] + "Preview_Page_1.jpg";
            }
            catch (ConfigurationErrorsException ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
            finally
            {
                // Hide the updating image
                _pictureBoxUpdating.Visible = false;
            }
        }
    }
}